/*
* @file model/pdtmanagement.js
*  产品管理
* @author
*
*/
import { routerRedux } from 'dva/router';
import api from '../api';
import { productType,origProductType } from '../config/dict';
import moment from 'moment';

const nbeginDate = moment(new Date()).format('YYYYMMDD');
const nendDate = moment(new Date()).format('YYYYMMDD');
export default {
	namespace: 'pdtmanagement',
	state:{
		// name:"",
	},
	subscriptions:{
		setup({dispatch,history}) {
			history.listen(({pathname,query}) => {
				if( pathname === '/systemMgt/accountMgt/pdtMgt' ){
					dispatch({
						type: 'whetherRequest',
						payload: query
					})
				}
			})
		}
	},
	effects:{
		* whetherRequest({
				payload
			}, {call, put,select}) {
				const state=yield select(state => state.pdtmanagement)
				const newObj={}
				for(var i in payload){
					newObj[i]=state[i]
				}
				
				if(_.isEmpty(payload)){
					yield put({
						type:'getPdtList',
						payload:{
							currentpage:1,
							pagesize:15,
						}
					})
				}else if(!_.isEmpty(payload)){
					if(JSON.stringify(payload)!=JSON.stringify(newObj)){
						yield put({
					 		type:'getPdtList',
					 		payload:{
					 			...payload,
					 			currentpage:1,
								pagesize:15,
					 		}
					 	})
					}
				}
		},
		* getPdtList({ payload }, { call, put }) {
			// console.log('getPdtList>',payload)
				yield put({type:'clearState'})
				yield put({
					type:'initData',
					payload:{
						pdtlist:[]
					}
				})
				payload.productType=['LICAI','GONGMU','MONI','SIMU']
			const response = yield call(api.getpdtList, payload);
			yield put({
				type: 'getListSuccess',
				payload: response ,
			});
		},
		*changeRoute({payload:query},{call,put}){
			yield put(routerRedux.push({
				pathname:`/systemMgt/accountMgt/pdtMgt`,
				query:{
					...query,
				}
			}))
		},
		* gotoViweposition({payload:query}, {call, put}){
			yield put(routerRedux.replace({
				pathname:`/systemMgt/accountMgt/pdtMgt/examine`,
				query:{
					productId:query.productId,
					productCode:query.productCode,
					beginDate:nbeginDate,
					endDate:nendDate
				}       
			}))
		},
		* gotoViwetrade({payload:query},{call,put}){
			yield put(routerRedux.push({
				pathname:`/systemMgt/accountMgt/pdtMgt/viewtrade`,
				query:{
					productId:query.productId,
					productCode:query.productCode,
					beginDate:nbeginDate,
					endDate:nendDate
				}       
			}))
		},
		* gotoViwefunds({payload:query},{call,put}){
			yield put(routerRedux.push({
				pathname:'/systemMgt/accountMgt/pdtMgt/viwefunds',
				query:{
					productId:query.productId,
					productCode:query.productCode,
				}
			}))
		},
		* addPdt({ payload }, { call, put }) {
			const response = yield call(api.addPdt, payload);
			yield put({
				type: 'addSuccess',
				payload: { response },
			});
			yield put({
				type: 'getPdtList',
				payload:{
					currentpage:1,
					pagesize:15,
				}
			});
		},
		* savePdtEdit({ payload }, { call, put }) {
			const response = yield call(api.savePdtEdit, { ...payload });
			yield put({
				type: 'saveSuccess',
				payload: { response },
			});
			yield put(routerRedux.goBack());
		},
		* removePdt({ payload}, { call, put }) {
			const productId = payload.productId;
			const response = yield call(api.removePdt,productId,{...payload} );
			yield put({
				type: 'delSuccess',
				payload: response ,
			});
			yield put({
				type: 'getPdtList',
				payload:{
					currentpage:1,
					pagesize:15,
				}
			});
		},
		* importfile({payload},{call,put}){
			const response = yield call(api.importfile, payload);
			yield put({
				type:'importSuccess',
				payload:{response}
			})
		},
		*uploadFiles({payload:query},{call,put}){
			const productId = query.productId;
			const type = query.type;
			const formData = query.formData;
			
		},
	},
	reducers:{
		clearState(state) {
			return{}
		},
		initData(state, {payload}) {
			// console.log('payload>1>>>>>',payload)
			return {
				...state,
				...payload
			}
		},
		getListSuccess(state, {payload}) {
			const {data,page} = payload
			data.forEach((item) => {
				item.productTypeText = productType[item.productType];
				item.origProductTypeText = origProductType[item.origProductType] || item.origProductType;
				item.effectiveDate = moment(item.effectiveDate).format('YYYY-MM-DD');
			});
			const status=data ? true:false
			return {
				...state,
				status,
				pdtlist:data,
				page,
			}
		},
		saveSuccess(state, action) {
			const { payload: { response } } = action;
			return {
				...state,
			};
		},
		addSuccess(state, action) {
			const { payload: { response } } = action;
			return {
				...state,
			};
		},
		delSuccess(state, action) {
			const { payload: { response } } = action;
			return {
				...state,
			};
		},
		importSuccess(state, action) {
			const { payload: { response } } = action;
			return {
				...state,
			};
		},
	}
}